package chapter13;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : DuJiabao
 * @Date : 2021/2/24 15:31
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class Groups {
  static public final String POEM = "Twas brillig, and the slithy toves\n" +
          "Did fadf fafafj faklfer jkju jkl\n" +
          "af 34 54536 ,6563 244\n" +
          "44 242  24543, g dg sf\n" +
          "254 da,  gsf  fgsfa";

  public static void main(String[] args) {
    Matcher m = Pattern.compile("(?m)(\\S+)\\s+(\\S+)\\s+(\\S+)$").matcher(POEM);  // 寻找所有句子的最后三个词
    while (m.find()) {
      // m.group有4个元素，组号为0表示整个表达式，组号1表示被第一个括号扩起的组，以此类推
      for (int i = 0; i <= m.groupCount(); i++) {
        System.out.print("[" + m.group(i) + "]");
      }
      System.out.println();
    }
  }
}
